<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\UserCoinOp;
use App\Http\Controllers\Controller;
use App\Model\SysConfigManage;
use App\Model\UserProfile;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class SysConfigManageController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('公共配置');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('公共配置编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建公共配置');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(SysConfigManage::class, function (Grid $grid) {
            $grid->model()->orderBy('sort', 'desc');
            $grid->title('标题');
            $grid->name('标识');
            $grid->value('值');
            $grid->remark('描述');
            $grid->sort('排序');
            $grid->status('状态');
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
//            $grid->filter(function ($filter) {
//                $filter->expand();
//                $filter->disableIdFilter();
//                $filter->column(1 / 4, function ($filter) {
//                    $filter->equal('userProfile.user_profile_id', '用户ID');
//                });
//            });
            // $grid->disableActions();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();

                $actions->append(new UserCoinOp($actions->getKey(), $actions->row, 'usercoin/op'));
            });
        });
    }

    /**
     * 新建
     * @return Form
     */
    protected function form()
    {
        return Admin::form(SysConfigManage::class, function (Form $form) {
            $form->text('title', '标题')->default('');
            $form->text('name', '标识');
            $form->text('value', '值')->default('');
            $form->textarea('remark', '描述')->default('');
            $form->text('sort', '排序')->default(1);
            $form->select('status', '是否开启')->options([0 => '开启', 1 => '禁用']);
        });
    }

}
